package com.sangeng.utils;

import com.sangeng.domain.vo.HotArticleVo;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class BeanCopyUtils {
    private BeanCopyUtils(){

    }

    /**
     * 单个实体拷贝
     * @param source
     * @param clazz
     * @param <V>
     * @return
     */
    public static <V> V copyBean(Object source,Class<V> clazz){
        V result=null;
        try {
            result=clazz.newInstance();
            BeanUtils.copyProperties(source,result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 拷贝实体集合
     */
    public static <O,V> List<V> copyBeanList(List<O> list,Class<V> clazz){
        List<V> list1=new ArrayList<>();
        for (Object list2:list){
            list1.add(copyBean(list2,clazz));
        }
        return list1;
    }
}
